package com.itheima.interceptor;

import com.itheima.entity.Result;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import static com.itheima.constant.MessageConstant.SYSTEM_MESSAGE_FAIL;

@Component
@ControllerAdvice
public class ProjectExceptionAdvice {
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result doException(Exception e) {
        //记录异常,发送邮件给开发人员
        e.printStackTrace();
        return Result.fail(-999, SYSTEM_MESSAGE_FAIL);
    }

    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody
    public Result doAccess(AccessDeniedException e) {
        //记录异常，实际工作中，给开发人员 发短信和邮件
        e.printStackTrace();
        return Result.fail(-999, "无权限");
    }
}

